package com.ljx.NoteBody.controller;

import com.ljx.NoteBody.common.R;
import com.ljx.NoteBody.dto.UserDto;
import com.ljx.NoteBody.entity.User;
import com.ljx.NoteBody.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/register")
    public R<String> register(@RequestBody UserDto userDto,
                              HttpServletRequest request){
        userService.register(userDto, request);
        return R.success("注册成功");
    }

    @PostMapping("/login")
    public R<Map<String, Object>> login(@RequestBody User user,
                                        HttpServletRequest request,
                                        HttpServletResponse response){
        return R.success(userService.login(user, request, response));
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest httpServletRequest){
        userService.logout(httpServletRequest);
        return R.success("退出成功");
    }

    @GetMapping("/{username}")
    public R<User> username(@PathVariable("username") String username,
                            HttpServletRequest httpServletRequest){
        return R.success(userService.queryUserByUsername(username, httpServletRequest));
    }

    @PutMapping
    public R<User> updateUserInfo(@RequestBody User user){
        return R.success(userService.updateUserInfo(user));
    }

}
